package com.shenhaofeng.app.http.response;

import com.shenhaofeng.app.http.CacheControl;
import com.shenhaofeng.app.http.Headers;
import com.shenhaofeng.app.http.adapter.Adapter;
import com.shenhaofeng.app.http.request.Request;

import java.io.IOException;


/**
 * 返回的响应对象
 * Created by Administrator on 2016/5/5.
 */
public class Response {

    private Request request;

    private Protocol protocol;

    private int code;

    private Headers header;

    private ResponseBody body;



    public Response(Builder builder) {
        this.request = builder.request;
        this.code = builder.code;
        this.protocol = builder.protocol;
        this.header = builder.headers;
        this.body = builder.responseBody;
    }


    public Response(Request request, Headers header, int code) {
        this.request = request;
        this.header = header;
        this.code = code;
    }


    public Request request() {
        return request;
    }


    public int code() {
        return code;
    }


    public boolean isSuccessful() {
        return code >= 200 && code < 300;
    }


    public Headers headers() {
        return header;
    }


    public ResponseBody body() {
        return body;
    }




    public static class Builder {

        private int code;

        private Request request;

        private Protocol protocol;

        private ResponseBody responseBody;

        private ResponseBody cacheResponseBody;

        private Headers headers;

        private CacheControl cacheControl;


        public Builder request(Request request) {
            this.request = request;
            return this;
        }


        public Builder code(int code) {
            this.code = code;
            return this;
        }


        public Builder protocol(Protocol protocol) {
            this.protocol = protocol;
            return this;
        }


        public Builder headers(Headers headers) {
            this.headers = headers;
            return this;
        }


        public Builder body(ResponseBody responseBody) {
            this.responseBody = responseBody;
            return this;
        }


        public Response build() {
            return new Response(this);
        }
    }
}
